<?php

/*
 * B 端 controller 父类
 */

namespace Think;

use Think\Controller;

class B_Controller extends Controller {
    
    public function __construct() {
        parent::__construct();
        import('Sessions');
        new Sessions();
        $p = D('Passport', 'Logic');
        if (!$p->isLogin()) {
            redirect('/home/passport/login');
        }
        if ( $_SESSION['id'] ){
            $this->s = D('Store', 'Logic');
            $this->a = D('Action', 'Logic');

            //$this->store = $this->s->getStoreByUserId();
            $this->user = $p->getUserById($_SESSION['id']);
            if($this->user['is_admin'] == IS_ADMIN){
                $c = D('Companystoremap', 'Logic');
                $arr = $c->getStoreIds($_SESSION['id']);
                if(count($arr)>0){
                    $this->store = $this->s->getStoresByIds($arr);
                }
            }else{
                $this->store = $this->s->getStores();
                foreach($this->store as $v){
                    $arr[] = $v['id'];
                }
            }
            /*if(count($arr)<1 && strstr('store',strtolower($_SERVER['REQUEST_URI']))){
                $this->_msg('_NOT_HAVE_SHOP_');
            }*/

            //未读消息
            // $arr = array("1"=>13,"2"=>22);
            $msg_num = 0;
            $_SESSION[$_SESSION['id'].'_storeid'] = $arr;
            if ( !empty($arr) )
            {
                $this->_msg = D('Message','Logic');
                $msg_num = $this->msgNum = $this->_msg->getStoreMessageNum( $arr );
            }
            $this->assign( "msg_num",$msg_num );


            $this->assign('_store',$this->store);
            $this->store_id = $arr;
            $this->assign('store_id',(array)$this->store_id);
        }

    }
    
    protected function _msg($msg){
        $this->assign('msg',L($msg));
        $this->display('msg');
        exit;
    }

}
